﻿using System;
using UnityEngine.UIElements;
namespace NodeEditor
{
    public class TextFieldBinding : IBinding
    {
        string value;
        TextField textField;
        public Action<string> onChanged;
        TextFieldBinding(TextField textField)
        {
            this.textField = textField;
            value = textField.text;
        }
        void IBinding.PreUpdate()
        {

        }

        void IBinding.Release()
        {

        }

        void IBinding.Update()
        {
            if(value != textField?.text)
            {
                value = textField?.text;
                onChanged?.Invoke(value);
            }
        }

        public static TextFieldBinding Binding(TextField textField,Action<string> update)
        {
            var binding = new TextFieldBinding(textField);
            binding.onChanged = update;
            textField.binding = binding;
            return binding;
        }
    }


  

}
